﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;

namespace MoneyManagerNET.Presentation//Cipbili.Presentation.Infrastructure
{
    using System.Diagnostics;
    using MoneyManagerNET.Presentation.ViewModel.Base;
    using Mvvm.Infrastructure;
    
    //=================================//

    public static class Util
    {
        static TraceSource trace = new TraceSource("MVVM");

        public static TViewModel ViewModel<TViewModel>(this IView<TViewModel> view) where TViewModel : class
        {
            return view.DataContext as TViewModel;
        }


        //Ask user to select from list
        public static T PromptSelect<T>(this ListViewModel<T> vm)where T:class
        {
            var r = vm.ShowDialog();
            T selected=default(T);
            if (r.HasValue && r.Value)
            {
                selected= vm.Selected;
            }
            return selected;
        }

        public static bool? ShowDialog(this IViewModel vm)
        {
            try
            {
                vm.LoadValues();

                var view = ViewLocator.Locate(vm) as Window;
                view.DataContext = vm;
                
                Trace.Indent();
                var result=view.ShowDialog();
                if(result.HasValue && result.Value)
                    vm.PutValues();
                return result; 
            }
            finally {
                Trace.Unindent();
            }
        }

        

        //static void WindowClosing(object o, CancelEventArgs evt)
        //{
        //    var ierr = o as IDataErrorInfo;
        //    if (ierr != null)
        //        evt.Cancel = !string.IsNullOrEmpty(ierr.Error);
        //}

        #region thought for design time viewModel and model mocking for know handcoding design time instances into DesignTimeViewModelSupplier

        //class ViewModelSource
        //{
        //    public FrameworkElement Target { get; set; }
        //    object _dataContext;

        //    public object DataContext
        //    {
        //        get { if (_dataContext == null && Target != null) { Util.GetMockViewModel(Target.GetType()); } return _dataContext; }
        //        set { _dataContext = value; }
        //    }

        //}

        //static Util()
        //{

        //    var a = DependencyProperty.Register("ViewModelSource", typeof(ViewModelSource), typeof(FrameworkElement));


        //}

        //public static object GetMockViewModel(object framework_element)
        //{
        //    //TODO: use IsAssignableFrom
        //    Type vmType = associated.Where(kv => kv.Value == framework_element.GetType()).FirstOrDefault().Value;
        //    var vm = Activator.CreateInstance(vmType);
        //    ViewModelBase vmb = vm as ViewModelBase;
        //    vmb.Model = SelectMock(vmb.GetModelType());

        //    return vm;
        //}

        //public static object SelectMock(Type TEntity)
        //{
        //    if ((TEntity) == typeof(Model.ITransaction)) return AppService.Storage.Select<Model.ITransaction>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.ISubCategory)) return AppService.Storage.Select<Model.ISubCategory>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.ICategory)) return AppService.Storage.Select<Model.ICategory>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.IStockInvestment)) return AppService.Storage.Select<Model.IStockInvestment>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.IStockAccount)) return AppService.Storage.Select<Model.IStockAccount>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.ICurrency)) return AppService.Storage.Select<Model.ICurrency>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.IContragent)) return AppService.Storage.Select<Model.IContragent>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.IBankAccount)) return AppService.Storage.Select<Model.IBankAccount>().FirstOrDefault();
        //    if ((TEntity) == typeof(Model.IAccount)) return AppService.Storage.Select<Model.IAccount>().FirstOrDefault();
            
        //    return null;
        //}
        #endregion

    }
   
}
